package com.example.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.api.common.ErrorCode;
import com.example.api.exception.BusinessException;
import com.example.api.mapper.InterfaceInfoMapper;
import com.example.api.model.dto.interfaceInfo.InterfaceInfoQueryRequest;
import com.example.api.model.entity.InterfaceInfo;
import com.example.api.model.vo.InterfaceInfoVo;
import com.example.api.service.InterfaceInfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author sonsn
 * @description 针对表【interface_info(接口信息)】的数据库操作Service实现
 * @createDate 2025-07-06 11:13:09
 */
@Service
public class InterfaceInfoServiceImpl extends ServiceImpl<InterfaceInfoMapper, InterfaceInfo>
        implements InterfaceInfoService {

    @Override
    public void validInterfaceInfo(InterfaceInfo interfaceInfo, boolean add) {
        // 判断接口信息对象是否为空,为空则抛出参数错误的异常
        if (interfaceInfo == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // 获取接口信息对象的名称
        String name = interfaceInfo.getName();

        // 如果是添加操作,所有参数必须非空,否则抛出参数错误的异常
        if (add) {
            if (StringUtils.isAnyBlank(name)) {
                throw new BusinessException(ErrorCode.PARAMS_ERROR);
            }
        }
        // 如果接口名称不为空且长度大于50,抛出参数错误的异常,错误信息为"名称过长"
        if (StringUtils.isNotBlank(name) && name.length() > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "名称过长");
        }
    }

    @Override
    public InterfaceInfoVo getVo(InterfaceInfo entity) {
        if (entity == null) {
            return null;
        }
        InterfaceInfoVo vo = new InterfaceInfoVo();
        BeanUtils.copyProperties(entity, vo);
        return vo;
    }

    @Override
    public List<InterfaceInfoVo> getVOList(List<InterfaceInfo> entities) {
        if (CollUtil.isEmpty(entities)) {
            return new ArrayList<>();
        }
        return entities.stream().map(this::getVo).collect(Collectors.toList());
    }

    @Override
    public LambdaQueryWrapper<InterfaceInfo> getQueryWrapper(InterfaceInfoQueryRequest queryRequest) {
        return new LambdaQueryWrapper<>();
    }


}




